package cn.zt.ztzx.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.zt.ztzx.model.dto.system.LoginDto;
import cn.zt.ztzx.model.dto.system.SysUserDto;
import cn.zt.ztzx.model.vo.common.Result;
import cn.zt.ztzx.model.vo.common.ResultCodeEnum;
import cn.zt.ztzx.model.vo.system.LoginVo;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zt.ztzx.domain.SysUser;
import cn.zt.ztzx.service.SysUserService;
import cn.zt.ztzx.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @author 86182
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2024-09-19 11:21:05
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    @Autowired
    //注入的StringRedisTemplate  默认注入的bean是RedistTempate<Object,Object>
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public Result listUser(SysUserDto sysRoleDto) {
        Page<SysUser> page = new Page<>(sysRoleDto.getPageNum(), sysRoleDto.getPageSize());
        LambdaQueryWrapper<SysUser> wrapper = Wrappers.<SysUser>lambdaQuery().like(StringUtils.checkValNotNull(sysRoleDto.getKeyword()), SysUser::getUsername, sysRoleDto.getKeyword())
                .ge(StringUtils.checkValNotNull(sysRoleDto.getCreateTimeBegin()), SysUser::getCreateTime, sysRoleDto.getCreateTimeBegin())
                .le(StringUtils.checkValNotNull(sysRoleDto.getCreateTimeEnd()), SysUser::getCreateTime, sysRoleDto.getCreateTimeEnd());
        this.page(page,wrapper);
        return Result.build(page, ResultCodeEnum.SUCCESS);
    }

    @Override
    public SysUser getuserinfo(String token) {
        //解析token
        if (StrUtil.isEmpty(token)){
            throw new RuntimeException(ResultCodeEnum.DATA_ERROR.getMessage());
        }
        String userJson = redisTemplate.opsForValue().get("user:login:" + token);
        SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
        return sysUser;
    }

    @Override
    public Result<LoginVo> login(LoginDto loginDto) {
        boolean empty = ObjectUtil.isEmpty(loginDto);
        if (empty){
            throw  new RuntimeException(ResultCodeEnum.DATA_ERROR.getMessage());
        }
        String userName = loginDto.getUserName();
        SysUser one = this.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, userName));
        if (ObjectUtil.isEmpty(one)){
            throw new RuntimeException(ResultCodeEnum.LOGIN_ERROR.getMessage());
        }
        String password = loginDto.getPassword(); //123456
        String digest = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!ObjectUtil.equals(one.getPassword(),digest)){
            //密码错误
            throw new RuntimeException(ResultCodeEnum.LOGIN_ERROR.getMessage());
        }
        LoginVo loginVo = new LoginVo();
        //通过redis保存token信息
        //生成token
        String token = IdUtil.simpleUUID();
        redisTemplate.opsForValue().set("user:login:"+token, JSON.toJSONString(one),30, TimeUnit.MINUTES);
        loginVo.setToken(token);
        loginVo.setRefresh_token("");
        return Result.build(loginVo,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result deleteUser(Long id) {
        boolean b = this.removeById(id);
        return b?Result.build(null,ResultCodeEnum.SUCCESS):Result.build(null,ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Result addUser(SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        boolean b = this.save(sysUser);
        return b?Result.build(null,ResultCodeEnum.SUCCESS):Result.build(null,ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Result updateUser(SysUser sysUser) {
        sysUser.setUpdateTime(new Date());
        boolean b = this.updateById(sysUser);
        return b?Result.build(null,ResultCodeEnum.SUCCESS):Result.build(null,ResultCodeEnum.SYSTEM_ERROR);
    }
}




